<?
	function drizzo_config_file_check() {
		if(file_exists(dirname(__FILE__) . "/config.php")) {
			return true;
		} else {
			echo "<p>It appear Drizzo has not been installed.  Please edit '/engine/config_sample.php' and rename it to 'config.php' then go to <a href=\"/drizzo/engine/install.php\" title=\"Run the Installer\">the install page</a>.</p>";
			die();
		}
	}
	
	function drizzo_install_check() {
		global $drizzo_options;
		
		if(!file_exists('config.php')) {
			$is_installed = 0;
		} else {
			require_once('config.php');
			require_once('functions_db.php');

			drizzo_db_connect();

			$drizzo_install_check = @mysql_query("SELECT * FROM `$drizzo_options` WHERE `name`='install_timestamp'");
			if(!mysql_errno()) {
				$is_installed = @mysql_num_rows($drizzo_install_check);
			} else {
				$is_installed = 0;
			}
		}

		return $is_installed;
	}


	function drizzo_update_version_number() {
		global $drizzo_options;

		drizzo_db_connect();

		$this_version = 0.4;
		$this_version_type = "alpha";

		$version_query = @mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='drizzo_version_number' ORDER BY `value` DESC LIMIT 1");
		$version_query_count = @mysql_num_rows($version_query);

		if($version_query_count == 0) {
			$version_insert = @mysql_query("INSERT INTO `$drizzo_options` SET `name`='drizzo_version_number',`value`='$this_version'");
			if($version_insert) {
				$version_check = true;
			} else {
				$version_check = false;
			}
			$ping_google = fopen("http://www.google.com/webmasters/sitemaps/ping?sitemap=" . domain . "sitemap.php",'r');
			fclose($ping_google);
		} elseif($drizzo_version_number != $this_version) {
			$version_update = @mysql_query("UPDATE `$drizzo_options` SET `value`='$this_version' WHERE `name`='drizzo_version_number'");
			if($version_update) {
				$version_check = true;
			} else {
				$version_check = false;
			}
			$ping_google = fopen("http://www.google.com/webmasters/sitemaps/ping?sitemap=" . domain . "sitemap.php",'r');
			fclose($ping_google);
		} elseif($version_query_count >= 1) {
			while($row = @mysql_fetch_assoc($version_query)) {
				$drizzo_version_number = $row['value'];
			}
			$version_check = true;
		} else {
			$version_check = false;
		}

		$version_type_query = @mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='version_type' ORDER BY `value` DESC LIMIT 1") or die(mysql_error());
		$version_type_query_count = @mysql_num_rows($version_type_query);

		if($version_type_query_count == 0) {
			$version_type_insert = @mysql_query("INSERT INTO `$drizzo_options` SET `name`='version_type',`value`='$this_version_type'");
			if($version_type_insert) {
				$version_check = true;
			} else {
				$version_check = false;
			}
		} elseif($version_type != $this_version_type) {
			$version_type_update = @mysql_query("UPDATE `$drizzo_options` SET `value`='$this_version_type' WHERE `name`='version_type'");
			if($version_type_update) {
				$version_check = true;
			} else {
				$version_check = false;
			}
		} elseif($version_type_query_count >= 1) {
			while($row = @mysql_fetch_assoc($version_type_query)) {
				$version_type = $row['value'];
			}
			$version_check = true;
		} else {
			$version_check = false;
		}
		return $version_check;
	}

	function drizzo_check_for_update() {
		global $drizzo_options;
		
		$version_check = @mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='drizzo_version_number' LIMIT 1");
		if(!mysql_errno()) {
			$drizzo_version_number = @mysql_fetch_assoc($version_check);
			$drizzo_version_number = $drizzo_version_number['value'];
		} else {
			$drizzo_version_number = 0;
		}

		$xml = file_get_contents("http://drizzoblog.com/feeds/releases.xml","r");
		if($xml) {
			$xml = trim($xml);
			$version_start = strpos($xml, "<version>");
			$version_stop = strpos($xml, "</version>");
			$version_length = $version_stop - $version_start;
			$new_drizzo_version_number = strip_tags(substr($xml, $version_start, $version_length));
			$status_start = strpos($xml, "<status>");
			$status_stop = strpos($xml, "</status>");
			$status_length = $status_stop - $status_start;
			$new_drizzo_version_status = strip_tags(substr($xml, $status_start, $status_length));
			$link_start = strpos($xml, "<download>");
			$link_stop = strpos($xml, "</download>");
			$link_length = $link_stop - $link_start;
			$new_link = strip_tags(substr($xml, $link_start, $link_length));
			if($new_drizzo_version_number > $drizzo_version_number) {
				return "<p class=\"alert\">Drizzo " . $new_drizzo_version_number . $new_drizzo_version_status . " is now available.<br /><a href=\"" . $new_link . "\">Click here to download.</a></p>";
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
function drizzo_update_this_install() {
	global $drizzo_posts, $drizzo_options, $drizzo_warehouse, $drizzo_trashheap, $drizzo_gallery, $drizzo_events, $drizzo_links;
	
	if($_POST['update_check']) { // If the user has decided to upgrade
		$updated = 0;
		$errors = null;
		if($_POST['netcast'] == 1) { // Change netcast to podcast
			$podcast_update_posts = @mysql_query("ALTER TABLE `$drizzo_posts` CHANGE `type` `type` ENUM( 'blog', 'review', 'page', 'podcast', 'aside' )");
			$podcast_update_warehouse = @mysql_query("ALTER TABLE `$drizzo_warehouse` CHANGE `type` `type` ENUM( 'blog', 'review', 'page', 'podcast', 'aside' )");
			$podcast_update_trashheap = @mysql_query("ALTER TABLE `$drizzo_trashheap` CHANGE `type` `type` ENUM( 'blog', 'review', 'page', 'podcast', 'aside' )");
			if(!mysql_errno($podcast_update_posts) && !mysql_errno($podcast_update_warehouse) && !mysql_errno($podcast_update_trashheap)) {
				$updated++;
			} else {
				$errors .= "netcast->podcast,";
			}
		}
		if($_POST['options_length'] == 1) { // Change options name length to 100 chars
			$options_length_update = @mysql_query("ALTER TABLE `$drizzo_options` CHANGE `name` `name` varchar(255), CHANGE `value` `value` varchar(255)");
			if(!mysql_errno()) {
				$updated++;
			} else {
				$errors .= "options length,";
			}
		}
		
		if($_POST['modules'] == 1) { // Add the default values for what modules are available
			$modules = array('module_blog','module_pages','module_podcasts','module_reviews','module_gallery','module_comments','module_events','module_links');
			$modules_query = @mysql_query("SELECT `ID`,`name` FROM `$drizzo_options` WHERE `name` LIKE '%module_%'");
			if(!mysql_errno()) {
				while($row = @mysql_fetch_assoc($modules_query)) {
					$db_modules[] = $row['name'];
				}
				$updates = array_diff($modules,$db_modules);
				if($updates) {
					foreach($updates as $update) {
						$module_query = @mysql_query("INSERT INTO `$drizzo_options` SET `name`='$update',`value`='1'");
						if(!mysql_errno()) {
							$module_count++;
						}
					}
				}
				if(count($updates) == $module_count) {
					$updated++;
				} else {
					$errors .= "default modules,";
				}
			}
		}
		
		if($_POST['gallery_basename'] == 1) {  // Add the default gallery_basename
			$gallery_basename_query = @mysql_query("INSERT INTO `$drizzo_options` SET `name`='gallery_basename',`value`='gallery'");
			if(!mysql_errno()) {
				$updated++;
			} else {
				$errors .= "gallery basename,";
			}
		}
		
		if($_POST['gallery_schema'] == 1) { // Alter the Gallery schema
			$gallery_schema_update = @mysql_query("ALTER TABLE `$drizzo_gallery` DROP `title`, DROP `description`, DROP `tags`, DROP `file`, DROP `file_size`, DROP `file_type`, DROP `thumb`, DROP `thumb_size`, DROP `thumb_type`, DROP `auto_thumb`, DROP `date_added`, DROP `owner`, ADD `post_id` INT NOT NULL, ADD `path` TEXT NOT NULL;");
			if(!mysql_errno()) {
				$post_schema_update = @mysql_query("ALTER TABLE `$drizzo_posts` CHANGE `type` `type` ENUM('blog','review','page','podcast','aside','image') NULL DEFAULT NULL");
				if(!mysql_errno()) {
					$updated++;
				} else {
					$errors .= "gallery schema,";
				}
			}
		}
		
		if($_POST['event_schema'] == 1) { // Alter the Events schema
			$events_schema_update = @mysql_query("ALTER TABLE `$drizzo_events` CHANGE `when` `start` VARCHAR(14) NOT NULL, ADD `end` VARCHAR(14) NULL AFTER `start`, ADD `status` ENUM( 'publish', 'draft' ) NOT NULL DEFAULT 'draft', ADD `permalink_name` VARCHAR(100) NULL AFTER `status`, ADD `timezone_offset` VARCHAR(6) NULL AFTER `end`, ADD `address` LONGTEXT AFTER `where`");
			if(!mysql_errno()) {
				@mysql_query("INSERT INTO `$drizzo_options` (`ID`,`name`,`value`,`owner`) VALUES ('','events_basename','events',NULL)");
				$updated++;
			} else {
				$errors .= "events schema,";
			}
		}
		
		if($_POST['link_schema'] == 1) { // Alter the Links schema
			@mysql_query("ALTER TABLE `$drizzo_links` ADD `image` TEXT NULL");
			if(!mysql_errno) {
				$updated++;
			} else {
				$errors .= "links schema,";
			}
		}
		
		if($updated == count($_POST)) {
			$updates = "<p class=\"save\">The updates were successful.</p>\n";
		} else {
			$updates = "<p class=\"alert\">The following updates could not be performed: " . rtrim($errors,',') . ".</p>\n";
		}
	} else { // Display the form to update this install
		// Netcast => Podcast
		$types_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_posts` LIKE '%type%'");
		if(!mysql_errno()) {
			$types_array = @mysql_fetch_assoc($types_lookup);
			if(strpos($types_array['Type'],"netcast")) { // Netcasts need to be replaced with Podcasts
				$changes[] = "netcast";
			}
		}
		
		// Make field size for options 255 chars instead of 32/100
		$options_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_options` LIKE 'name'");
		if(!mysql_errno()) {
			$options_meta = @mysql_fetch_assoc($options_lookup);
			if(!strpos($options_meta['Type'],"255")) { // Options name length needs to be changed
				$changes[] = "options_length";
			}
		}
		
		// Make sure that all default modules have entries
		$modules_lookup = @mysql_query("SELECT `ID` FROM `$drizzo_options` WHERE `name` LIKE '%module_%'");
		if(!mysql_errno()) {
			if(mysql_num_rows($modules_lookup) < 8) {
				$changes[] = "modules";
			}
		}
		
		// See if a gallery_basename has been set
		if(drizzo_global_options('gallery_basename') == false) {
			$changes[] = "gallery_basename";
		}
		
		// Change Gallery schema
		$gallery_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_gallery`");
		if(!mysql_errno()) {
			while($field = mysql_fetch_assoc($gallery_lookup)) {
				if($field['Field'] == 'path') {
					$gallery_schema = true;
				}
			}
			if(!isset($gallery_schema)) {
				$changes[] = 'gallery_schema';
			}
		}
		
		// Change Events schema
		$events_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_events` LIKE 'start'");
		if(mysql_errno() || @mysql_num_rows($events_lookup) == 0) {
			$changes[] = 'event_schema';
		}

		// Change Links schema
		$links_lookup = @mysql_query("SHOW COLUMNS FROM `$drizzo_links` LIKE 'image'");
		if(mysql_errno() || @mysql_num_rows($links_lookup) == 0) {
			$changes[] = 'link_schema';
		}
		
		if(count($changes) > 0) {
			$updates = "<form method=\"post\" action=\"\" name=\"drizzo_updater\"><p class=\"alert\">Drizzo needs to update its database. Do you want to perform the upgrade? <input type=\"hidden\" name=\"update_check\" value=\"1\" /><a href=\"javascript:void(0)\" onclick=\"document.drizzo_updater.submit();\" title=\"Perform the Upgrade\">Yes</a></p>";
			foreach($changes as $change) {
				$updates .= "<input type=\"hidden\" name=\"$change\" value=\"1\" />";
			}
			$updates .= "</form>\n";
		} else {
			$updates = false;
		}
	}
	return $updates;
}
?>
